//
//  DeviceInfoViewController.m
//  Lightthings
//
//  Created by Hero on 2024/11/16.
//

#import "DeviceInfoViewController.h"
#import "DeviceInfoModel.h"
#import "DeviceFouncModel.h"
#import "HomeItemModel.h"
@interface DeviceInfoViewController ()<FRQBluetoothManagerDelegate>
/* <#note#> */

@property (nonatomic, strong) DeviceInfoModel * deviceInfo;
@property (nonatomic, strong) HomeItemModel * xinlv, *yundong, *tiwen, *shuimian, *xueyang, *xueya, *huxilv, *xindiantu, * xuetang;

@end

@implementation DeviceInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.jy_title = @"设备名字";
    self.collectionInsets = kEdge(self.jy_bar_h, 0, 0, 0);
    
    
    [self.rightBtn setTitle:SWString(@"断开") forState:0];
    self.rightBtn.zh_action = kMsgForward(@"closeAction", WKProxy(self));
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    
    
    MainSectionModel * s = [[MainSectionModel alloc] init];
    [s sectionInsets:kEdge(0, 20, 20, 20) ls:10 is:10];
    
    DeviceInfoModel * info = [[DeviceInfoModel alloc] init];
    info.per = self.peripheral;
    [s.items addObject:info];
    _deviceInfo = info;
    _xinlv = [HomeItemModel cModelWithImage:@"icon_home_heart" Title:SWString(@"心率") Value:[self defaultValueString:@"0" Unit:@"bmp"] ViewColor:@"#FFEEF1"];
    _xinlv.layout.itemSize = CGSizeMake(kScreenWidth / 2- 25, 120);
    _xinlv.action = kRunCMDINFO(@"_show_HomeDataListView",@{@"type":@(RecordType_HeartReat), @"uuid":self.peripheral.identifier.UUIDString});
    [s.items addObject:_xinlv];
    
    _yundong = [HomeItemModel cModelWithImage:@"icon_home_sport" Title:SWString(@"运动记录") Value:[self defaultValueString:@"0" Unit:SWString(@"公里")] ViewColor:@"#FFF2E3"];
    _yundong.layout.itemSize = CGSizeMake(kScreenWidth / 2- 25, 120);
    [s.items addObject:_yundong];
    
    _xueyang = [HomeItemModel cModelWithImage:@"icon_home_blood_oxygen" Title:SWString(@"血氧") Value:[self defaultValueString:@"0" Unit:@"%"] ViewColor:@"#FFE7E4"];
    _xueyang.action = kRunCMDINFO(@"_show_HomeDataListView",@{@"type":@(RecordType_BloodOxygen), @"uuid":self.peripheral.identifier.UUIDString});
    _xueyang.layout.itemSize = CGSizeMake(kScreenWidth / 2- 25, 120);
    [s.items addObject:_xueyang];
    
    _xueya = [HomeItemModel cModelWithImage:@"icon_home_blood_pressure" Title:SWString(@"血压") Value:[self defaultValueString:@"0" Unit:@"mmHg"] ViewColor:@"#FFE7F1"];
    _xueya.action = kRunCMDINFO(@"_show_HomeDataListView",@{@"type":@(RecordType_HeartReat), @"uuid":self.peripheral.identifier.UUIDString});
    _xueya.layout.itemSize = CGSizeMake(kScreenWidth / 2- 25, 120);
    [s.items addObject:_xueya];
    
    NormalArrowModel * watchSet = [self cModelWithTitle:SWString(@"手表设置") Image:@"手表设置图标"];
    watchSet.action = kRunCMDINFO(@"_show_deviceSetList",@{@"type":@"watch", @"uuid":self.peripheral.identifier.UUIDString});
    [s.items addObject:watchSet];
     
    NormalArrowModel * healthSet = [self cModelWithTitle:SWString(@"健康数据设置") Image:@"健康数据设置"];
    healthSet.action = kRunCMDINFO(@"_show_deviceSetList",@{@"type":@"health", @"uuid":self.peripheral.identifier.UUIDString});
    [s.items addObject:healthSet];
    
    NormalArrowModel * otherSet = [self cModelWithTitle:SWString(@"其他功能设置") Image:@"其他设置"];
    otherSet.action = kRunCMDINFO(@"_show_deviceSetList",@{@"type":@"other", @"uuid":self.peripheral.identifier.UUIDString});
    [s.items addObject:otherSet];
    
    self.dataSources = @[s];
    
    [self readDeviceInfo];
    // Do any additional setup after loading the view.
}

- (NSMutableAttributedString *)defaultValueString:(NSString *)value Unit:(NSString *)unit {
    return [SWTool attS1:value s2:[NSString stringWithFormat:@"  %@",unit] f1:U_bfont(22) c1:KLColor(@"000000") f2:U_font(14) c2:KLColor(@"000000")];
}
- (NormalArrowModel *)cModelWithTitle:(NSString *)title Image:(NSString *)icon {
    NormalArrowModel * m = [NormalArrowModel createTitle:title size:CGSizeMake(kScreenWidth - 40, 55) hiddenArrow:NO];
    m.idfBlock = ^NSString * _Nonnull{
        return @"BleControlFunctionCell";
    };
    m.icon = icon;
    return m;
}

- (void)readDeviceInfo {
    [[WatchCommandTool sharedInstance] readRealTimeDataFromPeripheral:self.peripheral Block:^(BOOL success, unsigned long stepData, unsigned long caloriesData, unsigned long distanceData, unsigned long heartRateData, unsigned long electricData) {
        if (success) {
//            HomeStepItemModel * m = [[HomeStepItemModel alloc] init];
//            NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
//            m.dateStr = [SWTool timeWithYearMonthDayCountDown:[NSString stringWithFormat:@"%f",interval] Format:@"yyyy-MM-dd"];
//            if (self.recordType == RecordType_Step) {
//                m.valueStr = [NSString stringWithFormat:@"%lu%@",stepData,SWString(@"step")];
//            }else if (self.recordType == RecordType_Heat) {
//                m.valueStr = [NSString stringWithFormat:@"%.2f%@",caloriesData / 1000.0,@"kCal"];
//            }else if (self.recordType == RecordType_Distance ){
//                m.valueStr = [NSString stringWithFormat:@"%.2f%@",distanceData / 1000.0,@"Km"];
//            }
            self.deviceInfo.electric = electricData;
            [self reloadIfNeeded];
        }
        [self readRealTimeData];
    }];
}

/// 心率 步数 距离 热量
- (void)readRealTimeData {
    [WatchCommandTool.sharedInstance readRealTimeDataFromPeripheral:self.peripheral Block:^(BOOL success, unsigned long stepData, unsigned long caloriesData, unsigned long distanceData, unsigned long heartRateData, unsigned long electricData) {
        if (success) {
          
            self.huxilv.value = [self defaultValueString:[NSString stringWithFormat:@"%ld", heartRateData] Unit:SWString(@"次/分")];
            
            self.yundong.value = [self defaultValueString:[NSString stringWithFormat:@"%ld", distanceData] Unit:SWString(@"公里")];
            
            [self reloadIfNeeded];
        }
        [self readBloodOxygenData];
    }];
}
/// 血氧
- (void)readBloodOxygenData {
    [WatchCommandTool.sharedInstance readBloodOxygenData:^(BOOL success, NSArray * _Nonnull allData) {
        if (success) {
            NSString * itemStr = allData.firstObject;
            NSString * rateD = [itemStr substringFromIndex:8];
            NSInteger  bloodOxygen = strtoll([[NSString stringWithFormat:@"0x%@",rateD] UTF8String], 0, 16);
            self.xueyang.value = [self defaultValueString:[NSString stringWithFormat:@"%ld",bloodOxygen] Unit:@"%"];
            [self reloadIfNeeded];
        }
    } ToPeripheral:self.peripheral];
}

- (void)closeAction {
    [FRQBluetoothManager.shareManager closeAuotReconnectPeripheral:self.peripheral];
}
-(void)onBLEManagerDisconnect:(id<FRBleAbility>)ability peripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    [self back];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[FRQBluetoothManager shareManager] removeBleDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [FRQBluetoothManager.shareManager addBleDelegate:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
